Skip to content

Fix undefined haversineDistance function call in map refresh service#162

Merged
MrAlders0n merged 4 commits into
devfrom
copilot/fix-haversine-distance-call
Dec 30, 2025
Merged

Fix undefined haversineDistance function call in map refresh service#162
MrAlders0n merged 4 commits into
devfrom
copilot/fix-haversine-distance-call

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 30, 2025

The map refresh service was calling haversineDistance() which doesn't exist, causing ReferenceError: haversineDistance is not defined at line 882 and breaking GPS-based map updates.

Changes

  • Updated checkAndRefreshMap() to call calculateHaversineDistance() - the existing function already used throughout the codebase for geofence and distance validation
  • Renamed debug tag from [GEOFENCE] to [HAVERSINE] in calculateHaversineDistance() to better reflect that this utility function is used across multiple contexts (map refresh, geofence validation, ping distance, RX batching)
// Before
const distance = haversineDistance(
  mapRefreshService.lastRefreshLocation.lat,
  mapRefreshService.lastRefreshLocation.lon,
  lat,
  lon
);

// After
const distance = calculateHaversineDistance(
  mapRefreshService.lastRefreshLocation.lat,
  mapRefreshService.lastRefreshLocation.lon,
  lat,
  lon
);

This enables the 25m GPS movement threshold for map refreshes and adds [HAVERSINE] debug logging from the distance calculation utility.

Original prompt

Problem

The Map Refresh Service in content/wardrive.js is calling haversineDistance() which doesn't exist, causing this error:

Uncaught ReferenceError: haversineDistance is not defined
    at checkAndRefreshMap (wardrive.js:882:22)
    at onGpsPositionUpdateForMap (wardrive.js:909:3)

However, the codebase already has a calculateHaversineDistance() function defined at line 784-819 in content/wardrive.js on the dev branch that does exactly what we need.

Current Code (Broken)

In checkAndRefreshMap() function around line 882:

const distance = haversineDistance(  // ❌ This function doesn't exist
  mapRefreshService.lastRefreshLocation.lat,
  mapRefreshService.lastRefreshLocation.lon,
  lat,
  lon
);

Existing Function (Available)

The codebase already has this function at line 784:

function calculateHaversineDistance(lat1, lon1, lat2, lon2) {
  debugLog(`[GEOFENCE] Calculating Haversine distance: (${lat1.toFixed(5)}, ${lon1.toFixed(5)}) to (${lat2.toFixed(5)}, ${lon2.toFixed(5)})`);
  
  const R = 6371000; // Earth's radius in meters
  const toRad = (deg) => (deg * Math.PI) / 180;
  
  const dLat = toRad(lat2 - lat1);
  const dLon = toRad(lon2 - lon1);
  
  const a = 
    Math.sin(dLat / 2) * Math.sin(dLat / 2) +
    Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
    Math.sin(dLon / 2) * Math.sin(dLon / 2);
  
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  const distance = R * c;
  
  debugLog(`[GEOFENCE] Haversine distance calculated: ${distance.toFixed(2)}m`);
  return distance;
}

This function is used by:

  • validateGeofence() - geofence validation
  • validateMinimumDistance() - 25m ping distance validation

Fix Required

Update the checkAndRefreshMap() function to call calculateHaversineDistance() instead of haversineDistance():

const distance = calculateHaversineDistance(  // ✅ Use existing function
  mapRefreshService.lastRefreshLocation.lat,
  mapRefreshService.lastRefreshLocation.lon,
  lat,
  lon
);

Files to Modify

  • content/wardrive.js - Update checkAndRefreshMap() function to use calculateHaversineDistance()

Testing

After this fix:

  1. Map should refresh every 5 seconds when connected
  2. Map should refresh when GPS moves 25m from last refresh location
  3. No more haversineDistance is not defined errors in console
  4. Debug logs should show [GEOFENCE] tags from the distance calculation

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…kAndRefreshMap

Co-authored-by: MrAlders0n <55921894+MrAlders0n@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix reference to non-existent haversineDistance function Fix undefined haversineDistance function call in map refresh service Dec 30, 2025
Copilot AI requested a review from MrAlders0n December 30, 2025 04:01
@MrAlders0n
Copy link
Copy Markdown
Collaborator

@copilot ✅ Debug logs will show [GEOFENCE] tags from the distance calculation Is this still a good tag for this function?

Copilot AI and others added 2 commits December 30, 2025 04:04
Co-authored-by: MrAlders0n <55921894+MrAlders0n@users.noreply.github.com>
…Distance

Co-authored-by: MrAlders0n <55921894+MrAlders0n@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Dec 30, 2025

@copilot ✅ Debug logs will show [GEOFENCE] tags from the distance calculation Is this still a good tag for this function?

You're right! I've renamed the tag from [GEOFENCE] to [HAVERSINE] since the function is used in multiple contexts (map refresh, geofence validation, ping distance, RX batching). This makes the logs clearer. Fixed in 925a069.

@MrAlders0n MrAlders0n marked this pull request as ready for review December 30, 2025 04:09
Copilot AI review requested due to automatic review settings December 30, 2025 04:09
@MrAlders0n MrAlders0n merged commit 72da521 into dev Dec 30, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical bug where the map refresh service was calling a non-existent haversineDistance() function, causing a ReferenceError that broke GPS-based map updates. The fix corrects the function call to use the existing calculateHaversineDistance() function already used throughout the codebase.

  • Fixed undefined function call in map refresh service (line 882)
  • Renamed debug tags in calculateHaversineDistance() from [GEOFENCE] to [HAVERSINE] to better reflect its usage across multiple contexts (map refresh, geofence validation, distance validation, RX batching)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@MrAlders0n MrAlders0n deleted the copilot/fix-haversine-distance-call branch December 30, 2025 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants